<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="建筑名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="buildingId">
              <a-select v-model="model.buildingId" placeholder="请选择建筑(场地)"
                        :getPopupContainer="(target) => target.parentNode">
                <a-select-option v-for="item in buildingDataList" :key="item.value">{{ item.label }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="教室编码" :labelCol="labelCol" :wrapperCol="wrapperCol"  prop="classroomCode">-->
<!--              <a-input v-model="model.classroomCode" placeholder="请输入教室编码"  ></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="教室名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="classroomId">
              <a-select v-model="model.classroomId" placeholder="请选择所属教室"
                        :getPopupContainer="(target) => target.parentNode">
                <a-select-option v-for="item in classroomDataList" :key="item.value">{{ item.label }}</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="关系有效性" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="buildingClassroomStatus">
<!--              <a-input-number v-model="model.buildingClassroomStatus" placeholder="请输入关系有效性" style="width: 100%" />-->
              <a-select  v-model="model.buildingClassroomStatus"  placeholder="请选择有效性"  :getPopupContainer= "(target) => target.parentNode">
                <a-select-option :value="1">有效</a-select-option>
                <a-select-option :value="0">无效</a-select-option>
              </a-select>
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import {getBuildingDataList} from '@api/building'
  import {getClassroomDataList} from '@api/classroom'

  export default {
    name: 'RelBuildingClassroomForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        buildingClassroomDisabled: false, //是否是建筑教室维护调用该页面
        disableSubmit:false,
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           buildingId: [
              { required: true, message: '请输入建筑编码!'},
           ],
           classroomId: [
              { required: true, message: '请输入教室编码!'},
           ],
           tenantId: [
              { required: true, message: '请输入学校编码（租户ID）!'},
           ],
        },
        url: {
          add: "/relbuildingclassroom/relBuildingClassroom/add",
          edit: "/relbuildingclassroom/relBuildingClassroom/edit",
          queryById: "/relbuildingclassroom/relBuildingClassroom/queryById"
        }
        ,
        //获取下拉教室列表数据
        classroomOptions: [],
        //获取下拉选择建筑数据
        buildingDataList: [],
        //获取下拉选择教室
        classroomDataList: [],
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      this.getBuildingData();
      this.getClassroomData();
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      async getBuildingData() {
        let res = await getBuildingDataList()
        this.buildingDataList = []
        res.result.forEach(item => {
          this.buildingDataList.push({
            label: item.buildingName,
            value: item.id
          })
        })
      },
      async getClassroomData() {
        let res = await getClassroomDataList()
        this.classroomDataList = []
        res.result.forEach(item => {
          this.classroomDataList.push({
            label: item.classroomName,
            value: item.id
          })
        })
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>